package com.library.gq.dao.imlp;

import com.library.gq.dao.InsertDao;
import com.library.gq.entity.InsertEntity;
import com.library.gq.util.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InsertDaoImpl extends BaseDao implements InsertDao {
        String sql=null;//定义一个sql语句的变量名

    /**
     * 添加捐赠(donatebook)表中的数据
     *
     * @param name     :捐书人姓名
     * @param bookName :书籍名
     * @param author   :作者
     * @param type     :书籍类型
     * @param sum      :该书籍捐赠数量
     * @param money    :该书籍价格
     * @param time     :该书籍捐赠时间
     * @return
     */
    @Override
    public Integer insert(String name, String bookName, String author, Integer type, Integer sum, double money, String time) {
        sql="INSERT INTO donatebook(name,bookName,author,type,sumber,price,time)VALUES(?,?,?,?,?,?,?)";
        Integer count=executeUpdate(sql,name,bookName,author,type,sum,money,time);
        return count;
    }

    /**
     * 添加书籍(book)表中的数据
     * @param bookName:书名
     * @param type:类型编码
     * @param author:作者
     * @param sum:数量
     * @param money:价格
     * @param location:书位置
     * @param source:书籍来源
     * @param time:书籍加入时间
     * @return
     */
    @Override
    public Integer insert_book(String bookName, Integer type, String author, Integer sum, double money, String location, String source, String time) {
        sql= "INSERT INTO book(bookName,type,author,sum,price,location,source,jdate)VALUES(?,?,?,?,?,?,?,?)";
        Integer count=executeUpdate(sql,bookName,type,author,sum,money,location,source,time);
        return count;
    }

    /**
     * 进行书名进行查询
     *
     * @return
     */
    @Override
    public Integer select(String bookName){
        sql="SELECT count(bookName) FROM book WHERE bookName=?";
      rs=executeQuery(sql,bookName);
      Integer book_select=0;
        try {
            while (rs.next()){
                book_select=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return book_select;
    }

    /**
     * 根据书名添加数量
     *
     * @param sum
     * @param bookName
     * @return
     */
    @Override
    public Integer update(Integer sum, String bookName) {
        sql="UPDATE book SET sum=sum+? WHERE bookName=?";
        Integer update_book=executeUpdate(sql,sum,bookName);
        return update_book;
    }

    /**
     *
     * 查询总记录条数
     *
     * @return
     */
    @Override
    public Integer totalCount() {
        sql="SELECT count(1) FROM donatebook";
        rs=executeQuery(sql);
        int count=0;
        try {
            while (rs.next()){
                count=rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 按条件进行模糊查询
     *
     * @param name
     * @param start    :状态
     * @param pageSzie :页数
     * @return
     */
    @Override
    public List<InsertEntity> list(String name, Integer start, Integer pageSzie) {
        List<InsertEntity>list=new ArrayList<>();
        if(name.equals("全部")||name=="全部"){
            sql="SELECT id,name,bookName,author,type,sumber,price,time FROM donatebook LIMIT ?,?";
            rs=executeQuery(sql,start,pageSzie);
        }else if(!name.equals("全部")||name=="全部"){
            sql="SELECT id,name,bookName,author,type,sumber,price,time FROM donatebook where bookName LIKE CONCAT('%',?,'%') LIMIT ?,?";
            rs=executeQuery(sql,name,start,pageSzie);
        }
        try {
            while (rs.next()){
               Integer id=rs.getInt(1);
               String names=rs.getString(2);
               String bookName=rs.getString(3);
               String author=rs.getString(4);
               Integer type=rs.getInt(5);
               String types=null;
               if (type==0){
                   types="网络小说";
               }else if(type==1){
                   types="儿童益智";
               }else if(type==2){
                   types="学习资料";
               }else if(type==3){
                   types="经典名著";
               }
               Integer sumber=rs.getInt(6);
               Double price=rs.getDouble(7);
               String time=rs.getString(8);
               InsertEntity insertEntity=new InsertEntity(id,names,bookName,author,types,sumber,price,time);
                list.add(insertEntity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 根据id删除书籍
     *
     * @param id :书籍编码
     * @return
     */
    @Override
    public Integer delete(Integer id) {
        sql="DELETE FROM donatebook where id=?";
        Integer count=executeUpdate(sql,id);
        return count;
    }
}
